﻿/****************************************************************************
Copyright (c) 2014-2015 凌惊雪 微信:Lingjingxue 邮箱:lingjingxue@sina.com QQ:271487457

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/

using UtilPublic.ServerLoger;
using StackExchange.Redis;
using System.Collections.Generic;
using UtilLib;

namespace UtilPublic.Redis
{
    /// <summary>
    /// Set存储
    /// </summary>
    public partial class RedisLink
    {
        /// <summary>
        /// 添加
        /// </summary>
        /// <typeparam name="T1"></typeparam>
        /// <typeparam name="T2"></typeparam>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public void SetAdd<T1, T2>(T1 key, T2 value)
        {
            try { RedisDb.SetAdd($"{key}", value.ToString()); }
            catch { loger.Fatal($"SetAdd Error {key} {value}"); }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <typeparam name="T1"></typeparam>
        /// <typeparam name="T2"></typeparam>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public void SetRemove<T1, T2>(T1 key, T2 value)
        {
            try { RedisDb.SetRemove($"{key}", value.ToString()); }
            catch { loger.Fatal($"SetRemove Error {key} {value}"); }
        }
        /// <summary>
        /// 删除并返回随机一个
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        public void SetPop<T>(T key)
        {
            try { RedisDb.SetPop($"{key}"); }
            catch { loger.Fatal($"SetPop Error {key}"); }
        }
        /// <summary>
        /// 是否存在
        /// </summary>
        /// <typeparam name="T1"></typeparam>
        /// <typeparam name="T2"></typeparam>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool SetExist<T1, T2>(T1 key, T2 value)
        {
            try { return RedisDb.SetContains($"{key}", value.ToString()); }
            catch { loger.Fatal($"SetExist Error {key} {value}"); return false; }
        }
        /// <summary>
        /// 长度
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        public long SetLength<T>(T key)
        {
            try { return RedisDb.SetLength($"{key}"); }
            catch { loger.Fatal($"SetLength Error {key}"); return 0; }
        }
        /// <summary>
        /// 元素列表
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="result"></param>
        public void SetMembers<T>(T key, ref List<string> result)
        {
            try
            {
                var t = RedisDb.SetMembers($"{key}");
                foreach (var n in t)
                {
                    result.Add(n);
                }
            }
            catch
            {
                loger.Fatal($"SetMembers Error {key}");
            }
        }
        /// <summary>
        /// 元素列表
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="result"></param>
        public void SetMembers<T>(T key, ref List<int> result)
        {
            try
            {
                var t = RedisDb.SetMembers($"{key}");
                foreach (string n in t)
                {
                    result.Add(n.ToInt());
                }
            }
            catch
            {
                loger.Fatal($"SetMembers Error {key}");
            }
        }
        /// <summary>
        /// 元素列表
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="result"></param>
        public void SetMembers<T>(T key, ref List<long> result)
        {
            try
            {
                var t = RedisDb.SetMembers($"{key}");
                foreach (string n in t)
                {
                    result.Add(n.ToLong());
                }
            }
            catch
            {
                loger.Fatal($"SetMembers Error {key}");
            }
        }
        /// <summary>
        /// 随机元素
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="result"></param>
        public void SetRandomMember<T>(T key, out long result)
        {
            try { result = (long)RedisDb.SetRandomMember($"{key}"); }
            catch { loger.Fatal($"SetRandomMembers Error {key}"); result = 0; }
        }
        /// <summary>
        /// 随机多个元素
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="result"></param>
        /// <param name="count"></param>
        public void SetRandomMembers<T>(T key, out List<long> result, long count)
        {
            result = new List<long>();
            try
            {
                var t = RedisDb.SetRandomMembers($"{key}", count);
                foreach (string n in t)
                {
                    result.Add(n.ToLong());
                }
            }
            catch
            {
                loger.Fatal($"SetRandomMembers Error {key}");
            }
        }
        /// <summary>
        /// 清空
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        public bool SetClear<T>(T key)
        {
            try { return RedisDb.KeyDelete($"{key}"); }
            catch { loger.Fatal($"SetClear Error {key}"); return false; }
        }


    }
}